#ifndef _ABSTRACT_TIMER_THREAD_H
#define _ABSTRACT_TIMER_THREAD_H

#include "AbstractThread.h"
#include "TimeWaiter.h"

class AbstractTimerThread : public AbstractThread
{
public:
	AbstractTimerThread(unsigned long interval);
	virtual ~AbstractTimerThread(void);

	void SetInterval(unsigned long interval);

protected:
	virtual void DoAction();
	virtual void OnTime() = 0;
	
private:
	unsigned long m_interval;
	TimeWaiter m_timeWaiter;
};

template<class T>
class MemberFunctionTimerExecuter : public AbstractTimerThread
{
	typedef void (T::*MemberFunctionPtr)();
public:
	MemberFunctionTimerExecuter(unsigned long interval, MemberFunctionPtr memberFunctionPtr, T* memberObj)
	: AbstractTimerThread(interval)
	{
		m_memberFunctionPtr = memberFunctionPtr;
		m_memberObj = memberObj;
	}

	bool Stop(bool waitQuit = true)
	{
		Lock();
		m_memberObj = NULL;
		m_memberFunctionPtr = NULL;
		UnLock();
		return AbstractTimerThread::Stop(waitQuit);
	}

protected:
	virtual void OnTime()
	{
		Lock();
		if(m_memberObj != NULL && m_memberFunctionPtr != NULL)
			(m_memberObj->*m_memberFunctionPtr)();
		UnLock();
	}

private:
	MemberFunctionPtr m_memberFunctionPtr;
	T* m_memberObj;
};

#endif //_ABSTRACT_TIMER_THREAD_H

